package dd.prog2.exercises.set14;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToolBar;

@SuppressWarnings("serial")
public class PhotoViewer extends JFrame implements Observer, ActionListener{

	final JFileChooser fc = new JFileChooser();
	final static String BROWSE_CMD = "Browse";
	final static String PREV_CMD = "<";
	final static String NEXT_CMD = ">";
	
	ImagePanel imagePanel;
	JLabel filePath;
	JToolBar toolBar;
	JButton browseButton;
	JButton prevImageButton;
	JButton nextImageButton;
	
	FileFinder ff;
	File file;
	
	public PhotoViewer() {
		this.setTitle("Simple Photo Viewer");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(250, 250);
		this.setSize(640, 480);
		
		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setSize(new Dimension(this.getWidth(), 30));
		
		browseButton = new JButton("Browse");
		browseButton.setActionCommand(BROWSE_CMD);
		browseButton.addActionListener(this);
		toolBar.add(browseButton);
		
		prevImageButton = new JButton("<");
		prevImageButton.setActionCommand(PREV_CMD);
		prevImageButton.addActionListener(this);
		toolBar.add(prevImageButton);
		
		nextImageButton = new JButton(">");
		nextImageButton.setActionCommand(NEXT_CMD);
		nextImageButton.addActionListener(this);
		toolBar.add(nextImageButton);
		
		filePath = new JLabel();
		filePath.setText(" No File Selected...");
		toolBar.add(filePath);
		
		imagePanel = new ImagePanel(this);
		imagePanel.setSize(new Dimension(this.getSize()));
		
		this.add(toolBar, BorderLayout.NORTH);
		this.add(imagePanel, BorderLayout.CENTER);

	}
	
	public static void main(String[] args) {
		PhotoViewer pv = new PhotoViewer();
		pv.setVisible(true);
	}

	void selectPhoto(){
		file = fc.getSelectedFile();
		imagePanel.setImage(file);
		filePath.setText(file.getAbsolutePath());
		ff = new FileFinder(file.getParentFile());
		ff.setCurrent(file);
	}
	
	void selectNextPhoto(){
		file = ff.next(file);
		if(file != null){
			filePath.setText(file.getAbsolutePath());
			imagePanel.setImage(file);
		}
	}
	
	void selectPrevPhoto(){
		file = ff.prev(file);
		if(file != null){
			filePath.setText(file.getAbsolutePath());
			imagePanel.setImage(file);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
//		this.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			JButton button = (JButton) e.getSource();
			String cmd = button.getActionCommand();
			if (cmd == PhotoViewer.BROWSE_CMD){
				int returnVal = fc.showOpenDialog(this);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					this.selectPhoto();
				}
			}
			else if (cmd == PhotoViewer.NEXT_CMD)
				selectNextPhoto();
			else if (cmd == PhotoViewer.PREV_CMD) {
				selectPrevPhoto();
			}
		}
	}
	
}
